package com.cskaoyan.service;

import com.cskaoyan.bean.MarketAdmin;
import com.cskaoyan.bean.MarketAdminExample;
import com.cskaoyan.bean.common.BasePageInfo;
import com.cskaoyan.bean.common.CommonData;
import com.cskaoyan.mapper.MarketAdminMapper;
import com.cskaoyan.util.MyPageHelper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @author stone
 * @date 2022/08/13 09:29
 */
@Service
public class AdminServiceImpl implements AdminService{

    @Autowired
    MarketAdminMapper marketAdminMapper;

    @Override
    public CommonData<MarketAdmin> query(String username, BasePageInfo info) {
        //PageHelper.startPage(info.getPage(), info.getLimit());
        MyPageHelper.startPage(info);

        MarketAdminExample example = new MarketAdminExample();
        if (info.getSort() != null && info.getOrder() != null) {
            example.setOrderByClause(info.getSort() + " " + info.getOrder());
        }
        MarketAdminExample.Criteria criteria = example.createCriteria();
        if (username != null && !"".equals(username)) {
            criteria.andUsernameLike("%" + username + "%");
        }

        List<MarketAdmin> marketAdmins = marketAdminMapper.selectByExample(example);

        /*marketAdmins.stream().map(marketAdmin -> {

        }).collect(Collectors.toList());*/

        PageInfo<MarketAdmin> pageInfo = new PageInfo<>(marketAdmins);
        CommonData commonData = CommonData.data(pageInfo);
        return commonData;
    }
}
